import { defineStore } from 'pinia'
import api from '@/api'

export const useSupplyChainStore = defineStore('supplychain', {
  state: () => ({
    suppliers: [],
    purchaseOrders: [],
    inventory: []
  }),

  getters: {
    // 获取供应商列表
    getSuppliers: (state) => state.suppliers,
    
    // 获取采购订单列表
    getPurchaseOrders: (state) => state.purchaseOrders,
    
    // 获取库存列表
    getInventory: (state) => state.inventory
  },

  actions: {
    // 获取供应商列表
    async fetchSuppliers() {
      try {
        const response = await api.get('/api/supplychain/suppliers')
        if (response.data.code === 200) {
          this.suppliers = response.data.data
          return response.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        console.error('获取供应商列表失败:', error)
        throw error
      }
    },

    // 创建供应商
    async createSupplier(supplierData) {
      try {
        const response = await api.post('/api/supplychain/suppliers', supplierData)
        if (response.data.code === 200) {
          // 添加到本地状态
          this.suppliers.push(response.data.data)
          return response.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        console.error('创建供应商失败:', error)
        throw error
      }
    },

    // 更新供应商
    async updateSupplier(id, supplierData) {
      try {
        const response = await api.put(`/api/supplychain/suppliers/${id}`, supplierData)
        if (response.data.code === 200) {
          // 更新本地状态
          const index = this.suppliers.findIndex(supplier => supplier.id === id)
          if (index !== -1) {
            this.suppliers[index] = response.data.data
          }
          return response.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        console.error('更新供应商失败:', error)
        throw error
      }
    },

    // 删除供应商
    async deleteSupplier(id) {
      try {
        const response = await api.delete(`/api/supplychain/suppliers/${id}`)
        if (response.data.code === 200) {
          // 从本地状态移除
          this.suppliers = this.suppliers.filter(supplier => supplier.id !== id)
          return response.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        console.error('删除供应商失败:', error)
        throw error
      }
    },

    // 获取采购订单列表
    async fetchPurchaseOrders() {
      try {
        const response = await api.get('/api/supplychain/purchase-orders')
        if (response.data.code === 200) {
          this.purchaseOrders = response.data.data
          return response.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        console.error('获取采购订单列表失败:', error)
        throw error
      }
    },

    // 创建采购订单
    async createPurchaseOrder(orderData) {
      try {
        const response = await api.post('/api/supplychain/purchase-orders', orderData)
        if (response.data.code === 200) {
          // 添加到本地状态
          this.purchaseOrders.push(response.data.data)
          return response.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        console.error('创建采购订单失败:', error)
        throw error
      }
    },

    // 更新采购订单
    async updatePurchaseOrder(id, orderData) {
      try {
        const response = await api.put(`/api/supplychain/purchase-orders/${id}`, orderData)
        if (response.data.code === 200) {
          // 更新本地状态
          const index = this.purchaseOrders.findIndex(order => order.id === id)
          if (index !== -1) {
            this.purchaseOrders[index] = response.data.data
          }
          return response.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        console.error('更新采购订单失败:', error)
        throw error
      }
    },

    // 删除采购订单
    async deletePurchaseOrder(id) {
      try {
        const response = await api.delete(`/api/supplychain/purchase-orders/${id}`)
        if (response.data.code === 200) {
          // 从本地状态移除
          this.purchaseOrders = this.purchaseOrders.filter(order => order.id !== id)
          return response.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        console.error('删除采购订单失败:', error)
        throw error
      }
    },

    // 获取库存信息
    async fetchInventory() {
      try {
        const response = await api.get('/api/supplychain/inventory')
        if (response.data.code === 200) {
          this.inventory = response.data.data
          return response.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        console.error('获取库存信息失败:', error)
        throw error
      }
    }
  }
})